//
//  MyTabBarController.m
//  ForengnMenu
//
//  Created by @-@ on 16/1/14.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "MyTabBarController.h"

@interface MyTabBarController ()

@property float maxX;

@property UIView * senderView;

@property BOOL isOpen;

@end

@implementation MyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置最大滑动值
    self.maxX = 260.0f;

    //平滑手势
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    
    [self.view addGestureRecognizer:pan];
    
    
    self.senderView = [UIView new];
    self.senderView.frame = self.view.bounds;
    self.senderView.backgroundColor = [UIColor clearColor];
    
    //轻敲手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    //设置手指数量
    tap.numberOfTouchesRequired = 1;
    
    //设置触摸次数
    tap.numberOfTapsRequired = 1;
    
    //添加到ImageView上去
    [self.senderView addGestureRecognizer:tap];

    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headAction) name:@"pushSlideslip" object:nil];

}


-(void)headAction{
    
    
    [self menuSlider];
    
    
}
//实现手势方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    
    [self menuSlider];
    
 
    
}
-(void)menuSlider{
    
    if (_isOpen) {
        [self.senderView removeFromSuperview];
        _isOpen = NO;
    }else{
        [self.view addSubview:self.senderView];
        _isOpen = YES;
    }
    
    //动画块
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect rect = self.view.frame;
        
        if (rect.origin.x == self.maxX) {
            rect.origin.x = 0.0f;
        }else{
            rect.origin.x = self.maxX;
        }
        
        self.view.frame = rect;
    }];

}
//平移
-(void)panAction:(UIPanGestureRecognizer *)pan{
    
    //拿到手势持续变化累加直
    CGPoint changePoint = [pan translationInView:self.view];
    static CGPoint beganPoint;
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        beganPoint = self.view.center;
        
    }else if(pan.state==UIGestureRecognizerStateChanged){
        
        float x = beganPoint.x+changePoint.x;
        
        if (x <self.view.frame.size.width/2.0f) {//160
            x = self.view.frame.size.width/2.0f;//160
            
        }
        
        
        if (x > self.view.frame.size.width/2.0f + _maxX) {
            x = self.view.frame.size.width/2.0f + _maxX;
        }
        
        self.view.center = CGPointMake(x, self.view.center.y);
        
    }else if(pan.state==UIGestureRecognizerStateEnded){
        
        
        [UIView animateWithDuration:0.3 animations:^{
            
            CGRect rect = self.view.frame;
            
            if (self.view.frame.origin.x<150) {
                rect.origin.x = 0;
            }else{
                rect.origin.x=_maxX;
                [self.view addSubview:_senderView];
                _isOpen = YES;
            }
            self.view.frame=rect;
            
        }];
        
    }

    
}


@end
